package org.shinetour.sms.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.shinetour.sms.model.*;
import org.shinetour.sms.service.ISendSmsService;
import org.shinetour.sms.service.ISmsManageService;
import org.shinetour.sms.web.controller.BaseController;
import org.shinetour.sms.web.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 短信管理
 * @Author: ZhenHua Wang
 * @CreateDate: 2020/3/20 15:33
 */

@Controller
@RequestMapping("/smsManage")
public class SmsManageApiController extends BaseController {

    @Autowired
    ISmsManageService smsService;

    /**
     * @Description: 查询短信发送记录
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/1 17:47
     */
    @PostMapping("/listSmsRecord")
    @ResponseBody
    public ResultModel<TableDataInfo> listSmsRecordPage(@RequestParam Map<String, String> map) {
        ResultModel<TableDataInfo> res = new ResultModel<TableDataInfo>().success();
        try {
            Page<SmsRecord> page = smsService.listSmsRecord(map);
            res.setData(getTableDataInfo(page));
        } catch (Exception ex) {
            return res.error(ex.getMessage());
        }
        return res;
    }

    /**
     * @Description: 根据iid查询短信模板
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/16 9:43
     */
    @PostMapping("/selectSmsTemplateById")
    @ResponseBody
    public ResultModel<SmsTemplate> selectSmsTemplateById(@RequestParam Long iid) {
        ResultModel<SmsTemplate> res = new ResultModel<>();
        try {
            SmsTemplate template = smsService.selectSmsTemplateById(iid);
            res = res.success(template);
        } catch (Exception ex) {
            res = res.error(ex.getMessage());
        }
        return res;
    }

    /**
     * @Description: 查询模板列表
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/9 17:38
     */
    @PostMapping("/listSmsTemplate")
    @ResponseBody
    public ResultModel<TableDataInfo> listSmsTemplatePage(@RequestParam Map<String, String> map) {
        ResultModel<TableDataInfo> res = new ResultModel<TableDataInfo>().success();
        try {
            Page<SmsTemplate> page = smsService.listSmsTemplate(map);
            res.setData(getTableDataInfo(page));
        } catch (Exception ex) {
            return res.error(ex.getMessage());
        }
        return res;
    }

    /**
     * @Description: 查询模板参数
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/9 17:48
     */
    @PostMapping("/listSmsTemplateParams")
    @ResponseBody
    public ResultModel<List<TemplateParams>> listSmsTemplateParams(@RequestParam String templateCode) {
        ResultModel<List<TemplateParams>> res = new ResultModel<List<TemplateParams>>().success();
        try {
            List<TemplateParams> list = smsService.listSmsTemplateParams(templateCode);
            res.setData(list);
        } catch (Exception ex) {
            return res.error(ex.getMessage());
        }
        return res;
    }

    /**
     * @Description: 新增或编辑短信模板
     * @Author: ZhenHua Wang
     * @CreateDate: 2020/4/15 9:56
     */
    @PostMapping("/addOrEditTemplate")
    @ResponseBody
    public ResultModel addOrEditTemplate(@RequestBody TemplateDTO template) {
        ResultModel res = new ResultModel();
        try {
            res = smsService.addOrEditTemplate(template);
        } catch (Exception ex) {
            return res.error(ex.getMessage());
        }
        return res;
    }


}
